home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 206 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  93 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!eskimo!news
  3. From: mag@eskimo.com (mAg)
  4. Subject: Re: Pointers to structures
  5. X-Nntp-Posting-Host: tia1.eskimo.com
  6. Message-ID: <DKLDnH.96B@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: *.*
  9. X-Newsreader: WinVN 0.93.10
  10. References: <4cc9r4$m26@armitage.cyberspace.com>
  11. Date: Wed, 3 Jan 1996 06:21:16 GMT
  12.  
  13. In article <4cc9r4$m26@armitage.cyberspace.com> (2 Jan 1996 21:59:00 GMT), icarus@loomis 
  14. says :
  15. >
  16. >I'm having trouble using a pointer to a struct.  The idea is to have a 
  17. >struct get filled by a function, which gets the address and everything 
  18. >via function(struct type*).  Very straightforward.  However, if I try to 
  19. >CHANGE anything in the struct, it just goes back when the function is 
  20. >over.  So, let's pretend this imaginary structure is what I'm using:
  21. >
  22. >struct st1 {
  23. >  char *name;
  24. >  int yadda;
  25. >};
  26. >
  27. >And the function is:
  28. >
  29. >fn1(struct st1 *st)
  30. >{
  31. >  st = (struct st1*)malloc(sizeof(struct st1*));
  32. >  st->name = (char*)malloc(16);
  33. >  strcpy(st->name,"Test");
  34. >  st->yadda = 100;
  35. >}
  36. >
  37. >At the last line of code in fn1, all the values are correct;  st->name is 
  38. >"Test" and st->yadda is 100.  As soon as it exits, however, st->name 
  39. >points to NULL and st->yadda is zero.  The allocation of the structure in 
  40. >the first line of fn1 doesn't seem to matter;  both ways, I get NULL 
  41. >pointers coming out of my ears.
  42. >
  43.  
  44. the local copy of the pointer is getting alloced and lost in transit.
  45.  
  46. it will be better to write the function as follows :
  47.  
  48. #define FAILURE 0
  49. #define SUCCESS 1
  50. int fn1(struct st1 **ppst)
  51. {
  52. int iRetVal = FAILURE;
  53. if (*ppst = malloc(sizeof(struct st1)))
  54.   {
  55.   if ((*ppst)->name = (char*)malloc(16))
  56.     {  
  57.     (*ppst)strcpy(st->name,"Test");
  58.     (*ppst)->yadda = 100;
  59.     return(SUCCESS);
  60.     }
  61.   free(*ppst);
  62.   *ppst = NULL;
  63.   }
  64. return (FAILURE);
  65. }
  66.  
  67. and in the caller program
  68.  
  69. struct st1 *pst;
  70.  
  71. if (FAILURE == fn1(&pst))
  72.   {
  73. /* something failed in fn1 handle the error
  74.   }
  75. else
  76.   {
  77. /* now you can use pst->name and pst->yadda */
  78.   }
  79.  
  80. Please note that I have not even attempted to compile this code, there may be silly typos 
  81. here and there, but hope you get the idea.
  82.  
  83. And yes include the header file.
  84.  
  85. /* --------------------------------------------------------
  86.                       MAG@ESKIMO.COM
  87. http://www.eskimo.com/~mag/index.html
  88. ***********************************************************
  89. To understand recursion one must first understand recursion
  90. ***********************************************************
  91. -------------------------------------------------------- */
  92.  
  93.